package io.unicorn.modules.water.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.unicorn.common.validator.ValidatorUtils;
import io.unicorn.modules.sys.entity.SysDeptEntity;
import io.unicorn.modules.sys.entity.SysUserEntity;
import io.unicorn.modules.sys.service.SysDeptService;
import io.unicorn.modules.sys.shiro.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.unicorn.modules.water.entity.WaterdeptEntity;
import io.unicorn.modules.water.service.WaterdeptService;
import io.unicorn.common.utils.PageUtils;
import io.unicorn.common.utils.R;



/**
 * ${comments}
 *
 * @author LNCG
 * @email LNCG@USTL.EDU.CN
 * @date 2021-05-11 20:43:42
 */
@RestController
@RequestMapping("water/waterdept")
public class WaterdeptController {
    @Autowired
    private WaterdeptService waterdeptService;

    @Autowired
    SysDeptService sysDeptService;


    @RequestMapping("/query")
    //@RequiresPermissions("sys:dept:list")
    public List<WaterdeptEntity> list(){
        List<WaterdeptEntity> deptList = waterdeptService.queryWaterDept(new HashMap<String, Object>());

        return deptList;
    }

    @RequestMapping("/queryCityAreaUnit")
    //@RequiresPermissions("sys:dept:list")
    public List<WaterdeptEntity> listCityAreaUnit(){
        List<WaterdeptEntity> deptList = waterdeptService.queryDeptCityAreaUnit(new HashMap<String, Object>());

        return deptList;
    }

    /*2023.2.15 存在异常二级机构信息*/
    @RequestMapping("/queryDeptErrSubWater")
    //@RequiresPermissions("sys:dept:list")
    public List<WaterdeptEntity> listDeptSubWater(){
        SysUserEntity user = ShiroUtils.getUserEntity();
        SysDeptEntity dept = sysDeptService.getById(user.getDeptId());
        Long deptId =  Long.parseLong(dept.getOrderNum().toString());

        List<WaterdeptEntity> deptList = waterdeptService.queryDeptUnitSubWater(deptId);

        return deptList;
    }

    /*2023.4.3 存在异常二级机构待处理列表信息*/
    @RequestMapping("/queryDeptErrSubAlertWater")
    //@RequiresPermissions("sys:dept:list")
    public List<WaterdeptEntity> listDeptSubAlertWater(){
        SysUserEntity user = ShiroUtils.getUserEntity();
        SysDeptEntity dept = sysDeptService.getById(user.getDeptId());
        Long deptId =  Long.parseLong(dept.getOrderNum().toString());

        List<WaterdeptEntity> deptList = waterdeptService.queryDeptUnitAlertSubWater(deptId);

        return deptList;
    }


    /*2023.2.15 全部二级机构信息*/
    @RequestMapping("/queryDeptAllSubWater")
    //@RequiresPermissions("sys:dept:list")
    public List<WaterdeptEntity> listDeptAllSubWater(){
        SysUserEntity user = ShiroUtils.getUserEntity();
        SysDeptEntity dept = sysDeptService.getById(user.getDeptId());
        Long deptId =  Long.parseLong(dept.getOrderNum().toString());

        List<WaterdeptEntity> deptList = waterdeptService.queryWaterDeptAllSubWater(deptId);

        return deptList;
    }

    @RequestMapping("/queryDeptAll")
    //@RequiresPermissions("sys:dept:list")
    public List<WaterdeptEntity> listDeptAll(){
        SysUserEntity user = ShiroUtils.getUserEntity();
        Integer intViewData = user.getViewData();
        Map<String , Object> queryMap = new HashMap<>();
        queryMap.put("viewData",intViewData);

        List<WaterdeptEntity> deptList = waterdeptService.queryWaterDeptAll(queryMap);

        return deptList;
    }


    /**
     *
     */
    /*2023.2.15 全部二级机构信息*/
    @RequestMapping("/queryDeptTreeFgs")
    //@RequiresPermissions("sys:dept:list")
    public List<WaterdeptEntity> listDeptTreeFgs(){
        SysUserEntity user = ShiroUtils.getUserEntity();
        SysDeptEntity dept = sysDeptService.getById(user.getDeptId());
        String deptId =  '0'+dept.getOrderNum().toString();

        List<WaterdeptEntity> deptList = waterdeptService.queryWaterDeptTreeFgs(deptId);

        return deptList;
    }

    /*2023.4.24 存在异常二级机构信息*/
    @RequestMapping("/queryDeptErrTreeFgs")
    //@RequiresPermissions("sys:dept:list")
    public List<WaterdeptEntity> listDeptErrTreeFgs(){
        SysUserEntity user = ShiroUtils.getUserEntity();
        SysDeptEntity dept = sysDeptService.getById(user.getDeptId());
        String deptId =  '0'+ dept.getOrderNum().toString();

        List<WaterdeptEntity> deptList = waterdeptService.queryDeptUnitErrTreeFgs(deptId);

        return deptList;
    }
    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("water:waterdept:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = waterdeptService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{deptId}")
    //@RequiresPermissions("water:waterdept:info")
    public R info(@PathVariable("deptId") String deptId){
        WaterdeptEntity waterdept = waterdeptService.getById(deptId);

        return R.ok().put("waterdept", waterdept);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("water:waterdept:save")
    public R save(@RequestBody WaterdeptEntity waterdept){
        waterdeptService.save(waterdept);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("water:waterdept:update")
    public R update(@RequestBody WaterdeptEntity waterdept){
        ValidatorUtils.validateEntity(waterdept);
        waterdeptService.updateById(waterdept);
        
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("water:waterdept:delete")
    public R delete(@RequestBody String[] deptIds){
        waterdeptService.removeByIds(Arrays.asList(deptIds));

        return R.ok();
    }

}
